from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView, RetrieveAPIView, CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from spit.models import Spit
from spit.serializers import SpitSerializer, SpitDetailSerializer, SpitPublishSerializer
from rest_framework.permissions import IsAuthenticated


class SpitListView(ListAPIView):
    """吐槽列表"""
    serializer_class = SpitSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ('create_time')
    queryset = Spit.objects.all()


class SpitDetailView(RetrieveAPIView):
    """吐槽详情"""
    serializer_class = SpitDetailSerializer
    queryset = Spit.objects.all()


class SpitPublishView(CreateAPIView):
    """发布吐槽"""
    serializer_class = SpitPublishSerializer
    permission_classes = [IsAuthenticated]


class SpitLikeView(APIView):
    """吐槽点赞"""
    # permission_classes = [IsAuthenticated]

    def get(self, request, pk):
        user_id = request.user.id
        spit_liked_user_list = []


        spit = Spit.objects.get(id=pk)
        spit.spit_like_count += 1
        spit.save()

        return Response({"message":spit.spit_like_count})


# class SpitCollectionView(GenericAPIView):
#     """吐槽收藏"""
#     permission_classes = [IsAuthenticated]
#     serializer_class = SpitCollectionSerializer
#     queryset = SpitCollection.objects.all()

    # def post(self, r):







